<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">辽宁省公务用车管理平台</h3>
      </div>
      <div style="width: 450px; margin: 0px auto;">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            :autocomplete="autocomplete"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="code" />
          </span>
          <el-input
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="code"
            tabindex="3"
            auto-complete="off"
            @keyup.enter.native="handleLogin"
          />
          <identify class="code" :identifyCode="fakeCode" @click.native="changeCode"></identify>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:10px;"
          @click.native.prevent="handleLogin"
        >登 录</el-button>
        <el-row>
          <el-col class="forgetPwd">
            <span @click="forgetPwd">忘记密码</span>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <resetPwd ref="resetPwd"></resetPwd>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { login, getInfo } from "@/api/user";
import { applyListjson } from "@/api/usecar";
import { removeToken } from "@/utils/auth";
import { getToken } from "@/utils/auth";
import identify from "@/components/identify/index";
import resetPwd from "./components/reset-pwd";
export default {
  components: { identify, resetPwd },
  name: "Login",
  created() {
    this.changeCode();
    //开发时候开启自动填充密码
    if (this.NODE_ENV == "development") {
      this.autocomplete = "on";
    }
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (!value || value.length == 0 || value.trim().length == 0) {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        code: ""
      },
      autocomplete: "new-password",
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ],
        code: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value) {
                callback("请输入验证码");
              } else if (value != this.fakeCode) {
                callback("验证码有误");
              } else {
                callback();
              }
            }
          }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      fakeCode: "1949"
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    //忘记密码
    forgetPwd() {
      this.$refs.resetPwd.show();
    },
    //刷新验证码
    changeCode() {
      this.fakeCode = 1000 + Number.parseInt(Math.random() * 9000) + "";
      if (this.NODE_ENV == "development") {
        this.loginForm.code = this.fakeCode;
      }
    },
    async checkLoginState() {
      try {
        await this.$store.dispatch("user/getInfo");
        this.$router.push({ name: "Dashboard" });
      } catch (e) {
        console.log(e);
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    async handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true;
          try {
            //登录
            let r = await this.$store.dispatch("user/login", this.loginForm);
            //获取用户信息
            await this.$store.dispatch("user/getInfo", r);
            //获取左侧单位树
            await this.$store.dispatch("user/getLeftJson");
            this.$router.push({ path: this.redirect || "/" });
            this.loading = false;
          } catch (e) {
            console.log(e);
            this.loading = false;
            this.$message({
              message: (e && e.message) || "登录失败",
              type: "error"
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      // let r=await login({a:1})
      // if(r.cookie)
      // sessionStorage.setItem("logined","1")
      // this.$router.push({ path: this.redirect || "/" });
      // this.loading = false;
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px rgba(55, 152, 255, 0.5) inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #3798ff;
  background-image: url("../../assets/loginImage.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: center;
  overflow: hidden;

  .login-form {
    position: absolute;
    max-width: 100%;
    overflow: hidden;
    top: calc(50% - 263px);
    left: calc(50% - 291px);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: white;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      margin: 0 auto 40px;
      text-align: center;
      color: #3798ff;
      font-weight: 700;
      font-size: 48px;
      letter-spacing: 5px;
      text-shadow: 0 1px 0 #999, 0 2px 0 #888, 0 3px 0 #777, 0 4px 0 #666,
        0 5px 0 #555, 0 6px 0 #444, 0 7px 0 #333, 0 8px 7px #001135;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-form-item {
    border: 1px solid hsla(0, 0%, 100%, 0.1);
    background: rgba(55, 152, 255, 0.5);
    border-radius: 5px;
    color: #000;
  }
  .code {
    position: absolute;
    top: 7px;
    right: 10px;
    cursor: pointer;
    user-select: none;
  }
  .forgetPwd {
    color: white;
    font-size: 14px;
    text-shadow: #666 1px 1px;
    cursor: pointer;
  }
}
</style>
